package bank;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JLabel;
import javax.swing.JTextField;

public class CustomerRegistDialog extends JDialog {
	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private AccountModel accountModel;

	/**
	 * Create the dialog.
	 */
	public CustomerRegistDialog() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(5, 1, 0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			{
				JLabel lblNewLabel = new JLabel("이름");
				panel.add(lblNewLabel);
			}
			{
				textField = new JTextField();
				panel.add(textField);
				textField.setColumns(10);
			}
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			{
				JLabel label = new JLabel("나이");
				panel.add(label);
			}
			{
				textField_1 = new JTextField();
				textField_1.setColumns(10);
				panel.add(textField_1);
			}
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			{
				JLabel label = new JLabel("주번");
				panel.add(label);
			}
			{
				textField_2 = new JTextField();
				textField_2.setColumns(10);
				panel.add(textField_2);
			}
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			{
				JLabel label = new JLabel("주소");
				panel.add(label);
			}
			{
				textField_3 = new JTextField();
				textField_3.setColumns(10);
				panel.add(textField_3);
			}
		}
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel);
			{
				JLabel label = new JLabel("계좌");
				panel.add(label);
			}
			{
				textField_4 = new JTextField();
				textField_4.setColumns(10);
				panel.add(textField_4);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent paramActionEvent) {
						if(!checkJubun(textField_2.getText())){
							JOptionPane.showMessageDialog(getContentPane(), "주민번호자리수는 13자리입니다.(숫자)", "asd" , 1, null);
							return;
						}
						AccountModel accountModel2 = new AccountModel();
						accountModel2.setName(textField.getText());
						accountModel2.setAge(textField_1.getText());
						accountModel2.setSsn(textField_2.getText());
						accountModel2.setAddress(textField_3.getText());
						accountModel2.setAccountNumber(textField_4.getText());
						
						setAccountModel(accountModel2);
						
						JOptionPane.showMessageDialog(getContentPane(), "등록완료", "등록완료" ,1,null);
						dispose();
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		setVisible(true);
	}
	
	public boolean checkJubun(String jubun){
		Pattern compile = Pattern.compile("[0-9]{13,13}");
		
		Matcher m = compile.matcher(jubun);
		
		if(m.find()){
			String group = m.group(0);
			if(group.equals(jubun))
				return true;
		}
		return false;
	}
	
	public void setAccountModel(AccountModel ac){
		accountModel = ac;
	}
	
	public AccountModel getAccountModel(){
		return accountModel;
	}

}
